# -*- coding: utf-8 -*-
# created on 2017/1/16


from sympy.abc import t
from sympy import sympify, diff
import re
import numpy as np


# 判断:y=a*x+b or y=a*x
def yici_judge(*args):
    if len(args) == 2:
        f, symbol = args
    else:
        raise Exception("to do")
    f_diff = diff(f, symbol)
    if f_diff.has(symbol):
        return False
    else:
        return True


# 求向量的项
def vector_xiangs(poly, args=None):
    if args is None:
        args = []
    p = r'vector\(.{1,3}?\)'
    m = re.findall(p, str(poly))
    m = list(set(m))
    new_m = []
    for item in m:
        new_m.append(sympify(item))
    return new_m


# 求向量的系数：2*a+3*b
def vector_coeffs(poly):
    p = r'vector\(.{1,2}?\)'
    m = re.findall(p, str(poly))
    m = list(set(m))
    xians = []
    for item in m:
        xians.append(sympify(item))
    coeffs = {}
    for xiang in xians:
        xiang_coeff = poly.coeff(xiang)
        coeffs[xiang] = xiang_coeff
    return coeffs


def vector_judge_linear(poly):
    p = r'vector\(.{1,2}?\)'
    m = re.findall(p, str(poly))
    m = list(set(m))
    xians = []
    for item in m:
        xians.append(sympify(item))
    for xiang in xians:
        new_poly = poly
        new_poly = new_poly.subs({xiang: t})
        if not yici_judge(new_poly, t):
            answer = False
            return answer
    for xiang in xians:
        poly = poly.subs({xiang: t})
    if not yici_judge(poly, t):
        answer = False
    else:
        answer = True
    return answer


def vector_get_abs(poly):
    abs_xiangs = []
    p1 = r'Abs_[a-zA-Z]_[0-9]'
    m1 = re.findall(p1, str(poly))
    if m1:
        for item1 in m1:
            abs_xiangs.append(sympify(item1))
    else:
        p2 = r'Abs_[a-zA-Z][a-zA-Z]'
        m2 = re.findall(p2, str(poly))
        if m2:
            for item2 in m2:
                abs_xiangs.append(sympify(item2))
        else:
            p3 = r'Abs_[a-zA-Z]'
            m3 = re.findall(p3, str(poly))
            if m3:
                for item3 in m3:
                    abs_xiangs.append(sympify(item3))
    abs_xiangs = set(abs_xiangs)
    abs_xiangs = list(abs_xiangs)
    return abs_xiangs


def vector_get_angles(poly):
    c_angles_xiangs = []
    p1 = r'CAngle_[a-zA-Z]_[0-9][a-zA-Z]_[0-9]'
    m1 = re.findall(p1, str(poly))
    if m1:
        for item1 in m1:
            c_angles_xiangs.append(sympify(item1))
    else:
        p2 = r'CAngle_[a-zA-Z][a-zA-Z]'
        m2 = re.findall(p2, str(poly))
        if m2:
            for item2 in m2:
                c_angles_xiangs.append(sympify(item2))
    c_angles_xiangs = set(c_angles_xiangs)
    c_angles_xiangs = list(c_angles_xiangs)
    return c_angles_xiangs


def vector_axis_names(vectors):
    names = []
    for vector in vectors:
        name = vector.args[0]
        name = str('Axis_') + str(name)
        names.append(name)
    return names


def vector_abs_names(vectors):
    names = []
    for vector in vectors:
        name = vector.args[0]
        name = str('Abs_') + str(name)
        names.append(name)
    return names


def vector_abs_known_update(known, vectors_abs_names):
    for vectors_Abs_name in vectors_abs_names:
        if vectors_Abs_name not in known:
            known[vectors_Abs_name] = sympify(vectors_Abs_name)
    return known


def xl_dimension(vector):
    lens = []
    for key, item in vector.items():
        lens.append(len(item))
    lens = set(lens)
    lens = list(lens)
    if len(lens) == 1:
        length = lens[0]
    else:
        length = None
    return length


def xl_eqlist_2_array(conditions, length=None):
    new_conditions = {}
    for key, item in conditions.items():
        new_conditions[key] = np.array(item)
    return new_conditions


if __name__ == '__main__':
    pass
